package com.socket.io.client;

import com.alibaba.fastjson.JSON;
import com.socket.io.msg.Msg;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @auther zhangwj
 * @date 2020/9/23 2:59 下午
 */
public class IOClient {

    public static void main(String[] args) {
        new Thread(new Client()).start();
    }
}

class Client implements Runnable {

    @Override
    public void run() {
        Socket socket = null;
        try {
            socket = new Socket("127.0.0.1", 8080);
            Msg msg = new Msg();
            msg.setClientId(2);
            msg.setSendClientId(1);
            msg.setMsg("我发送了数据耶");
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            out.println(JSON.toJSONString(msg));
            out.close();
            socket.close();
        } catch (IOException e) {
            System.out.println("异常");
        } finally {
            if (null != socket) {
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("关闭Socket异常");
                }
            }
        }

    }
}